

ALTER    PROCEDURE sp_close_rma_proc

 @RMA_ID         INT

AS
BEGIN

	declare @totalLineItems int
	declare @closeLineItems int
	declare @rmaCloseDate datetime
	
	
	select @totalLineItems = count(*) from RMA_Line_Item_Table where RMA_ID = @RMA_ID 
	select @closeLineItems = count(*) from RMA_Line_Item_Table where RMA_ID = @RMA_ID AND Status = 'CLOSE'

	if ( @totalLineItems <> 0 and @closeLineItems = @totalLineItems )
	begin
		select @rmaCloseDate = max(Close_Date) from RMA_Line_Item_Table where RMA_ID = @RMA_ID
		update RMA_Table set Close_Date = @rmaCloseDate , RMA_Status = 'CLOSE' where RMA_ID = @RMA_ID	 
	end

END



